/*
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2023-11-28 15:48:43
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2024-02-22 19:49:19
 * @FilePath: \h5-components-demo\src\router\index.ts
 * @Description: 路由配置
 */
import { createRouter, createWebHistory } from 'vue-router'

const fileMap = import.meta.glob('@/views/**/**.vue')
const firstRoutes = Array.from(new Set(Object.keys(fileMap).map((item) => item.split('/')[3])))

const routes:any[] = []
firstRoutes.forEach((firstRoute) => {
  const children = Object.entries(fileMap).filter((child) => child[0].indexOf(`/${firstRoute}/`) > -1)
  routes.push({
    path: firstRoute,
    name: firstRoute,
    children: children.map((child) => {
      const secondRoute = child[0].split('/')[4].split('.')[0]
      return {
        path: secondRoute,
        name: `${firstRoute}-${secondRoute}`,
        component: child[1]
      }
    })
  })
})

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '',
      name: '',
      redirect: '/button/demo1',
      children: [
        ...routes
        /*
        {
          path: 'button',
          name: 'button',
          children: [
            { path: 'demo1', name: 'button-demo1', component: () => import('@/views/button/demo1.vue') },
            { path: 'demo2', name: 'button-demo2', component: () => import('@/views/button/demo2.vue') },
            { path: 'demo3', name: 'button-demo3', component: () => import('@/views/button/demo3.vue') },
            { path: 'demo4', name: 'button-demo4', component: () => import('@/views/button/demo4.vue') },
            { path: 'demo5', name: 'button-demo5', component: () => import('@/views/button/demo5.vue') },
            { path: 'demo6', name: 'button-demo6', component: () => import('@/views/button/demo6.vue') },
            { path: 'demo7', name: 'button-demo7', component: () => import('@/views/button/demo7.vue') },
            { path: 'demo8', name: 'button-demo8', component: () => import('@/views/button/demo8.vue') },
            { path: 'demo9', name: 'button-demo9', component: () => import('@/views/button/demo9.vue') },
            { path: 'demo10', name: 'button-demo10', component: () => import('@/views/button/demo10.vue') }
          ]
        },
        {
          path: 'action-sheet',
          name: 'action-sheet',
          children: [
            { path: 'demo1', name: 'action-sheet-demo1', component: () => import('@/views/action-sheet/demo1.vue') },
            { path: 'demo2', name: 'action-sheet-demo2', component: () => import('@/views/action-sheet/demo2.vue') },
            { path: 'demo3', name: 'action-sheet-demo3', component: () => import('@/views/action-sheet/demo3.vue') }
          ]
        },
        */
      ]
    }
  ]
})

export default router
